package com.alogic.uid.impl;

import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.Script;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.anysoft.util.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.w3c.dom.Element;
import java.util.HashMap;

/**
 * 增强Snowflake
 *
 * @since 1.6.13.5 [20200605 duanyy]
 */
public class SnowflakeExt extends Snowflake{
    @Override
    public void configure(Element e, Properties p) {
        Properties props = new XmlElementProperties(e,p);
        configure(props);
        Element onPreprareElem = XmlTools.getFirstElementByPath(e, "on-init");
        if (onPreprareElem != null){
            Logiclet onInit = Script.create(onPreprareElem, props);
            if (onInit != null){
                LogicletContext logicletContext = new LogicletContext(props);
                try {
                    XsObject doc = new JsonObject("root", new HashMap<String, Object>());
                    onInit.execute(doc, doc, logicletContext, null);
                    pId = PropertiesConstants.getLong(logicletContext,"$pid",pId);
                } catch (Exception ex) {
                    LOG.info("Failed to execute onInit script" + ExceptionUtils.getStackTrace(ex));
                }
            }
        }
    }
}